﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using condortravel.operaciones.core.DataModel;
using condortravel.operaciones.entities.Entities;
using System.Data.Linq;

namespace condortravel.operaciones.persistence.operacionesImpl
{
    public class EventoExtraordinarioDAOImpl : condortravel.operaciones.persistence.Impl.GenericDAO<Tb_EventoExtraordinario, int>
    {
        public override Tb_EventoExtraordinario update(Tb_EventoExtraordinario entity)
        {

            Tb_EventoExtraordinario evento;
            using (ImpersonationHelper imp = new ImpersonationHelper(this.Impersonation.User, this.Impersonation.Domain, this.Impersonation.Password))
            {
                if (IsImpersonate)
                {
                    if (!imp.ImpersonateUser())
                    {
                        throw new Exception("The Impersonate has failed");
                    }

                }

                using (DataContext dataContext = this.getInstanceDataContextFactory())
                {
                    evento = dataContext.GetTable<Tb_EventoExtraordinario>().Where(p => p.eventoExt_ID == entity.eventoExt_ID).Single();
                    evento.agente_ID = entity.agente_ID;
                    evento.detalle = entity.detalle;
                    evento.estadoEventExt_ID = entity.estadoEventExt_ID;
                    evento.eventoExt_ID = entity.eventoExt_ID;
                    evento.fecha = entity.fecha;
                    evento.hora = entity.hora;
                    evento.tipoEventExt_ID = entity.tipoEventExt_ID;
                                       dataContext.SubmitChanges();
                }
            }
            return evento;
        }

        public override Tb_EventoExtraordinario getByID(int id)
        {
            using (ImpersonationHelper imp = new ImpersonationHelper(this.Impersonation.User, this.Impersonation.Domain, this.Impersonation.Password))
            {
                if (IsImpersonate)
                {
                    if (!imp.ImpersonateUser())
                    {
                        throw new Exception("The Impersonate has failed");
                    }

                }
                using (DataContext dataContext = this.getInstanceDataContextFactory())
                {
                    DataLoadOptions options = new DataLoadOptions();
                    options.LoadWith<Tb_EventoExtraordinario>(s => s.Tb_Agente);
                    dataContext.LoadOptions = options;
                    return (from p in dataContext.GetTable<Tb_EventoExtraordinario>()
                            where p.eventoExt_ID == id
                            select p).SingleOrDefault<Tb_EventoExtraordinario>();

                }
            }
        }

        public override void delete(int pk)
        {
            string deleteDetails = string.Format("delete  from Tb_EventoExtraordinario where eventoExt_ID='{0}'", pk);
            this.executeSQL(deleteDetails);
        }

        public List<Tb_EventoExtraordinario> getListEventosExtByCriteria(FilterEventoExtCriteria filter)
        {
            using (ImpersonationHelper imp = new ImpersonationHelper(this.Impersonation.User, this.Impersonation.Domain, this.Impersonation.Password))
            {
                if (IsImpersonate)
                {
                    if (!imp.ImpersonateUser())
                    {
                        throw new Exception("The Impersonate has failed");
                    }

                }
                using (DataContext dataContext = this.getInstanceDataContextFactory())
                {
                    DataLoadOptions options = new DataLoadOptions();
                    options.LoadWith<Tb_EventoExtraordinario>(s => s.Tb_Estado_EventoExtraordinario);
                    options.LoadWith<Tb_EventoExtraordinario>(s => s.Tb_TipoEvento_Extraordinario);
                    
                    dataContext.LoadOptions = options;

                    return (from p in dataContext.GetTable<Tb_EventoExtraordinario>()
                            where
                             ((p.fecha >= filter.FecInicio && filter.FecInicio != DateTime.MinValue) || (filter.FecInicio == DateTime.MinValue)) &&
                            ((p.fecha <= filter.FecFin && filter.FecFin != DateTime.MinValue) || (filter.FecFin == DateTime.MinValue))    &&
                            ((p.estadoEventExt_ID == filter.Estado && filter.Estado != 0) || (filter.Estado == 0)) &&
                            ((p.tipoEventExt_ID == filter.Tipo && filter.Tipo != 0) || (filter.Tipo == 0))
                            select p).ToList<Tb_EventoExtraordinario>();
                }

            }
        }

    }
}
